#coding:utf-8


import  tensorflow as tf


with tf.variable_scope("layer1") as scope:

    v1=tf.get_variable('v',[1,2])
    scope.reuse_variables()
    v2=tf.get_variable('v',[1,2])
    v3 = tf.get_variable('v', [1, 2])
    print(v1.name)
    print(v2.name)
    print(v3.name)

print('-------------------')

with tf.variable_scope("foo") as foo_scope:
    v=tf.get_variable("v",[1])
    print('11 ',foo_scope.name)
    print('11 ',v.name)

with tf.variable_scope("foo"):
    v=tf.get_variable("w",[1])
    print('12 ', foo_scope.name)
    print('12 ', v.name)

with tf.variable_scope("foo") as foo_scope:
    assert foo_scope.name == "foo"
    print('1',foo_scope.name)

with tf.variable_scope("bar"):
    with tf.variable_scope("baz") as other_scope:
        assert other_scope.name == "bar/baz"
        print('2', other_scope.name)
        with tf.variable_scope(foo_scope) as foo_scope2:
            assert foo_scope2.name == "foo" # 保持不变
            print('3', foo_scope2.name)
            foo_scope2.reuse_variables()
            v2 = tf.get_variable('v')
            print(v2.name)
